package com.qdxwx.manage.controller.order;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.order.OrderDetailService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.manager.Manager;
import com.qdxwx.models.order.OrderDetail;
import com.qdxwx.models.order.OrderDetailQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 订单流水Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/order/detail")
public class OrderDetailController extends BaseController {

    @Autowired
    private OrderDetailService orderDetailService;

    @Permissions("order:detail:list")
    @GetMapping("/list.html")
    public String list() {
        return "order/order-detail-list";
    }

    /**
     * 查询订单流水列表
     */
    @Permissions("order:detail:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Manager manager = SessionUtils.getManager();
        if (manager == null){
            return TableDataInfo.error("您未登录！");
        }
        int searchType = TKConvert.toInteger(request.getParameter("searchType"));
        String searchKey = TKString.trim(request.getParameter("searchKey"));
        OrderDetailQuery query = new OrderDetailQuery();
        if(manager.getCompanyId() > 1){
            query.setCompanyId(manager.getCompanyId());
        }
        if (searchKey.length()>0) {
            int infoId = TKConvert.toInteger(searchKey);
            if (searchType == 0) {
                if (infoId > 0) {
                    query.setOrderId(infoId);
                } else {
                    query.setOrderNo(searchKey);
                }
            } else {
                if (infoId > 0) {
                    query.setCourseId(infoId);
                } else {
                    query.setCourseName(searchKey);
                }
            }
        }
        List<OrderDetail> list = orderDetailService.getList(query, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(orderDetailService.count(query));
        return rspData;
    }
}
